package com.usian.controller;

import com.usian.pojo.Product;
import com.usian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("user")
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("findAll")
    public String findAll(Model model){
        List<Product> users=userService.findAll();
        model.addAttribute("user",users);
        return "user";
    }

    @RequestMapping("userUp")
    public String userUp(Integer id,Model model){
        if(id==null || id==0){
            return "userAdd";
        }else {
            Product product=userService.selectId(id);
            model.addAttribute("user",product);
            return "userUp";
        }
    }

    @RequestMapping("userDel")
    public String userDel(Integer id){
        userService.delete(id);
        return "redirect:/user/findAll";
    }

    @RequestMapping("userAdd")
    public String userAdd(Product product){
        userService.insert(product);
        return "redirect:/user/findAll";
    }
    @RequestMapping("userUpdate")
    public String userUpdate(Product product){
        userService.update(product);
        return "redirect:/user/findAll";
    }
    @RequestMapping("userQY")
    public String userQY(Integer id){
        userService.qy(id);
        return "redirect:/user/findAll";
    }
    @RequestMapping("userSh")
    public String userSh(Model model){
        List<Product> users = userService.sh();
        model.addAttribute("user",users);
        return "userSh";
    }
    @RequestMapping("userS")
    public String userS(Integer id){
        userService.s(id);
        return "redirect:/user/userSh";
    }

}
